﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MTRC.ODMS.DAL;
using System.Data;
using AjaxControlToolkit;

namespace MTRC.ODMS.Web.SOM.UserControl
{
    public partial class PassengerSelectionCriteria : MTRC.ODMS.Web.UserControl.BaseUserControl
    {

        #region Function Property

        public string MenuID
        {
            get
            {
                return string.Concat(this.ViewState["_menuID"]);
            }
            set
            {
                this.ViewState["_menuID"] = value;
            }
        }

        public string AllAccessStationID
        {
            get
            {
                return string.Concat(this.ViewState["_allAccessStationID"]);
            }
            set
            {
                this.ViewState["_allAccessStationID"] = value;
            }
        }

        public string OtherAccessStationID
        {
            get
            {
                return string.Concat(this.ViewState["_otherAccessStationID"]);
            }
            set
            {
                this.ViewState["_otherAccessStationID"] = value;
            }
        }

        #endregion

        #region Properties

        public int UserQueryCriteriaID
        {
            get
            {
                return Convert.ToInt32(this.ViewState["_UserQueryCriteriaID"]);
            }
            set
            {
                this.ViewState["_UserQueryCriteriaID"] = value;
            }
        }

        public int LocationID
        {
            get
            {
                return Convert.ToInt32(this.ViewState["_LocationID"]);
            }
            set
            {
                this.ViewState["_LocationID"] = value;
            }
        }

        public int? FlagCurrentLocation
        {
            get
            {
                return Convert.ToInt32(this.ViewState["_FlagCurrentLocation"]);
            }
            set
            {
                this.ViewState["_FlagCurrentLocation"] = value;
            }
        }

        public int? FlagAllLocation
        {
            get
            {
                return Convert.ToInt32(this.ViewState["_FlagAllLocation"]);
            }
            set
            {
                this.ViewState["_FlagAllLocation"] = value;
            }
        }

        public int? FlagMoreLocation
        {
            get
            {
                return Convert.ToInt32(this.ViewState["_FlagMoreLocation"]);
            }
            set
            {
                this.ViewState["_FlagMoreLocation"] = value;
            }
        }

        public string DateRangeType
        {
            get
            {
                return string.Concat(this.ViewState["_DateRangeType"]);
            }
            set
            {
                this.ViewState["_DateRangeType"] = value;
            }
        }

        public int? FlagDateType
        {
            get
            {
                return Convert.ToInt32(this.ViewState["_FlagDateType"]);
            }
            set
            {
                this.ViewState["_FlagDateType"] = value;
            }
        }

        public string DateFrom
        {
            get
            {
                return Convert.ToString(this.ViewState["_DateFrom"]);
            }
            set
            {
                this.ViewState["_DateFrom"] = value;
            }
        }

        public string DateTo
        {
            get
            {
                return Convert.ToString(this.ViewState["_DateTo"]);
            }
            set
            {
                this.ViewState["_DateTo"] = value;
            }
        }

        #endregion

        public void BindUcPsc(string MenuID, string AllAccessStationID, string OtherAccessStationID)
        {
            this.MenuID = MenuID;
            this.AllAccessStationID = AllAccessStationID;
            this.OtherAccessStationID = OtherAccessStationID;
            //update by benson 2015-10-10 U181 begin
            Session["station"] = null;
            //update by benson 2015-10-10 U181 end
            SetLanguage();
            InitCriteriaData();
            BindddlCriteria(this.ODMUser.UserGroupID, this.MenuID);
            FunctionItemSetting();
        }

        #region Initial
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetLanguage();
                InitCriteriaData();
                BindddlCriteria(this.ODMUser.UserGroupID, this.MenuID);
                FunctionItemSetting();

                //Add by jack 20160125 ISU11 fixed for harcode 'Default Criteria'
                DataTable dtDefaultCriteria = MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetDefaultCriteriaNameByMenuID(this.MenuID);
                if (dtDefaultCriteria.Rows.Count > 0)
                {
                    this.ViewState["DefaultCriteriaName"] = dtDefaultCriteria.Rows[0]["CriteriaName"];
                }
                //End add by jack 20160125 ISU11 fixed for harcode 'Default Criteria'
            }

            if (ddlStation.Items.Count == 0)
            {
                ScriptManager.RegisterStartupScript(Page, typeof(string), "", "InitCombobox();SetCursorEnabled(true);", true);
                //this.btnSave.Enabled = false;
                //this.btnSaveas.Enabled = false;
                //this.btnDelete.Enabled = false;
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, typeof(string), "", "InitCombobox();SetCursorEnabled(false);", true);
            }
        }
        #endregion

        #region Event

        /// <summary>
        /// Add Criteria data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveas_Click(object sender, EventArgs e)
        {
            /* Comment by jack 20150910 for U180.5 */
            //ToolkitScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "", "OpenPopupCriteriaNew();", true);

            //string criteriaName = this.txtSelectedOU.Text;
            //int criteriaId = ExistCriteriaName(criteriaName, this.MenuID, this.ODMUser.UserGroupID);
            //if (criteriaName.Trim() == "" || string.IsNullOrEmpty(criteriaName))
            //{
            //    this.PAlert(this.GetMessage("SOM_PassengerFlow_CriteriaNotNullOrEmpty"));
            //    return;
            //}
            //else if (criteriaName.Trim() == "Default Criteria")
            //{ //default criteria
            //    this.PAlert(this.GetMessage("SOM_PassengerFlow_DefaultCriteriaCanNotCreate"));
            //    return;
            //}
            //else if (!(criteriaId > 0))
            //{
            //    //Add CriteriaData;
            //    Model.SOM.PassengerFlow.UserQueryCriteria userQueryCriteriaModel = new Model.SOM.PassengerFlow.UserQueryCriteria();
            //    DataTable dtLocation = new DataTable();
            //    dtLocation.Columns.Add("UserQueryCriteriaID");
            //    dtLocation.Columns.Add("LocationID");

            //    userQueryCriteriaModel.CriteriaName = criteriaName;
            //    userQueryCriteriaModel.MenuID = this.MenuID;
            //    userQueryCriteriaModel.UserGroupID = this.ODMUser.UserGroupID;
            //    userQueryCriteriaModel.FlagCurrentLocation = (ddlStation.SelectedItem.Value == "0") ? 0 : 1;
            //    userQueryCriteriaModel.FlagAllLocation = (ddlStation.SelectedItem.Value == "0") ? 1 : 0;
            //    if (ddlStation.SelectedItem.Value == "0")
            //    {
            //        if (this.ODMUser.LocationID == -1)
            //        {//Illegal Station
            //            this.PAlert(this.GetMessage("SOM_LegalStationCheck"));
            //            return;
            //        }
            //        userQueryCriteriaModel.LocationID = this.ODMUser.LocationID;
            //    }
            //    else
            //    {
            //        userQueryCriteriaModel.LocationID = Convert.ToInt32(ddlStation.SelectedValue);
            //    }

            //    userQueryCriteriaModel.FlagMoreLocation = rdbmorestation.Checked ? 1 : 0;
            //    if (userQueryCriteriaModel.FlagMoreLocation == 1)
            //    {

            //        string locationIDKey = this.hdnPersonHoldStationKye.Value;
            //        if (locationIDKey.Contains(","))
            //        { //more one location
            //            string[] locationIDArray = locationIDKey.Split(',');
            //            for (int i = 0; i < locationIDArray.Length; i++)
            //            {
            //                DataRow dr = dtLocation.NewRow();
            //                dr["UserQueryCriteriaID"] = 0; //0 is default value,after add criteria,get criteriaID then insert into table userquerysation 
            //                dr["LocationID"] = Convert.ToUInt32(locationIDArray[i]);
            //                dtLocation.Rows.Add(dr);
            //            }
            //        }
            //        else
            //        {//one location
            //            DataRow dr = dtLocation.NewRow();
            //            dr["UserQueryCriteriaID"] = 0; //0 is default value,after add criteria,get criteriaID then insert into table userquerysation 
            //            dr["LocationID"] = locationIDKey;
            //            if (!string.IsNullOrEmpty(locationIDKey))
            //            {
            //                dtLocation.Rows.Add(dr);
            //            }
            //        }

            //    }

            //    userQueryCriteriaModel.FlagDateType = rdbDateRange.Checked ? 1 : 0;
            //    if (userQueryCriteriaModel.FlagDateType == 1)
            //    {
            //        userQueryCriteriaModel.DateRangeType = ddlDateRange.SelectedValue;
            //        userQueryCriteriaModel.DateFrom = this.dtbFrom.GetDateTime();
            //        userQueryCriteriaModel.DateTo = this.dtbTo.GetDateTime();
            //    }
            //    else
            //    {
            //        userQueryCriteriaModel.DateRangeType = "";
            //        userQueryCriteriaModel.DateFrom = this.dtbFrom.GetDateTime();
            //        userQueryCriteriaModel.DateTo = this.dtbTo.GetDateTime();
            //    }

            //    userQueryCriteriaModel.LastUpdTime = DateTime.Now;
            //    userQueryCriteriaModel.LastUpdUser = this.ODMUser.ID;
            //    userQueryCriteriaModel.ActingasUser = this.ODMUser.ID;

            //    DAL.SOM.PassengerFlow.PassengerSelectionCriteria.AddCriteriaData(userQueryCriteriaModel, dtLocation);

            //    BindddlCriteria(this.ODMUser.UserGroupID, this.MenuID);

            //    this.FindByTextAndSelect(ddlCriteria, this.txtSelectedOU.Text);
            //    this.PAlert(this.GetMessage("SaveSuccessfully"));
            //    Session.Remove("station");
            //}
            //else
            //{
            //    this.PAlert(this.GetMessage("SOM_PatronageExcelReport_CannotCreateExists"));
            //    return;
            //}
        }

        protected void btnChangeCriteria_Click(object sender, EventArgs e)
        {

            string criteriaName = this.txtSelectedOU.Text.Trim();

            //Add CriteriaData;
            Model.SOM.PassengerFlow.UserQueryCriteria userQueryCriteriaModel = new Model.SOM.PassengerFlow.UserQueryCriteria();
            DataTable dtLocation = new DataTable();
            dtLocation.Columns.Add("UserQueryCriteriaID");
            dtLocation.Columns.Add("LocationID");

            userQueryCriteriaModel.CriteriaName = criteriaName;
            userQueryCriteriaModel.MenuID = this.MenuID;
            userQueryCriteriaModel.UserGroupID = this.ODMUser.UserGroupID;
            //update by gordon 20151009 U180
            //userQueryCriteriaModel.FlagCurrentLocation = (ddlStation.SelectedItem.Value == "0") ? 0 : 1;
            userQueryCriteriaModel.FlagCurrentLocation = (ddlStation.SelectedItem.Value == "0" || ddlStation.SelectedItem.Value != this.ODMUser.LocationID.ToString()) ? 0 : 1;
            //------------------------------
            userQueryCriteriaModel.FlagAllLocation = (ddlStation.SelectedItem.Value == "0") ? 1 : 0;
            if (ddlStation.SelectedItem.Value == "0")
            {
                if (this.ODMUser.LocationID == -1)
                {//illegal Station
                    this.PAlert(this.GetMessage("SOM_LegalStationCheck"));
                    return;
                }
                userQueryCriteriaModel.LocationID = this.ODMUser.LocationID;
            }
            else
            {
                userQueryCriteriaModel.LocationID = Convert.ToInt32(ddlStation.SelectedValue);
            }

            userQueryCriteriaModel.FlagMoreLocation = rdbmorestation.Checked ? 1 : 0;
            if (userQueryCriteriaModel.FlagMoreLocation == 1)
            {

                string locationIDKey = this.hdnPersonHoldStationKye.Value;
                if (locationIDKey.Contains(","))
                { //more one location
                    string[] locationIDArray = locationIDKey.Split(',');
                    for (int i = 0; i < locationIDArray.Length; i++)
                    {
                        DataRow dr = dtLocation.NewRow();
                        dr["UserQueryCriteriaID"] = 0; //0 is default value,after add criteria,get criteriaID then insert into table userquerysation 
                        dr["LocationID"] = Convert.ToUInt32(locationIDArray[i]);
                        dtLocation.Rows.Add(dr);
                    }
                }
                else
                {//one location
                    DataRow dr = dtLocation.NewRow();
                    dr["UserQueryCriteriaID"] = 0; //0 is default value,after add criteria,get criteriaID then insert into table userquerysation 
                    dr["LocationID"] = locationIDKey;
                    if (!string.IsNullOrEmpty(locationIDKey))
                    {
                        dtLocation.Rows.Add(dr);
                    }
                }

            }

            userQueryCriteriaModel.FlagDateType = rdbDateRange.Checked ? 1 : 0;
            if (userQueryCriteriaModel.FlagDateType == 1)
            {
                userQueryCriteriaModel.DateRangeType = ddlDateRange.SelectedValue;
                userQueryCriteriaModel.DateFrom = this.dtbFrom.GetDateTime();
                userQueryCriteriaModel.DateTo = this.dtbTo.GetDateTime();
            }
            else
            {
                userQueryCriteriaModel.DateRangeType = "";
                userQueryCriteriaModel.DateFrom = this.dtbFrom.GetDateTime();
                userQueryCriteriaModel.DateTo = this.dtbTo.GetDateTime();
            }

            userQueryCriteriaModel.LastUpdTime = DateTime.Now;
            userQueryCriteriaModel.LastUpdUser = this.ODMUser.ID;
            userQueryCriteriaModel.ActingasUser = this.ODMUser.ID;

            DAL.SOM.PassengerFlow.PassengerSelectionCriteria.AddCriteriaData(userQueryCriteriaModel, dtLocation);

            BindddlCriteria(this.ODMUser.UserGroupID, this.MenuID);

            this.FindByTextAndSelect(ddlCriteria, this.txtSelectedOU.Text);
            this.PAlert(this.GetMessage("SaveSuccessfully"));
            Session.Remove("station");
        }

        /// <summary>
        /// Update Criteria data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            /* Comment by jack 20150910 for U180.4*/
            #region old logic
            //string criteriaName = this.txtSelectedOU.Text;
            //if (criteriaName.Trim() == "" || string.IsNullOrEmpty(criteriaName))
            //{
            //    this.PAlert(this.GetMessage("SOM_PassengerFlow_CriteriaNotNullOrEmpty"));
            //    return;
            //}
            //if (criteriaName.Trim() == "Default Criteria")
            //{
            //    this.PAlert(this.GetMessage("SOM_PassengerFlow_DefaultCriteriaCanNotUpdate"));
            //    return;
            //}

            //int criteriaId = ExistCriteriaName(criteriaName, this.MenuID, this.ODMUser.UserGroupID);
            //if (criteriaId > 0) //Exists 
            //{
            //    DataTable dtLocation = new DataTable();
            //    dtLocation.Columns.Add("UserQueryCriteriaID");
            //    dtLocation.Columns.Add("LocationID");
            //    DataTable dtCriteria = DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetCriteriaByCriteriaID(criteriaId);

            //    Model.SOM.PassengerFlow.UserQueryCriteria userQueryCriteriaModel = new Model.SOM.PassengerFlow.UserQueryCriteria();
            //    userQueryCriteriaModel.UserQueryCriteriaID = criteriaId;
            //    userQueryCriteriaModel.CriteriaName = this.txtSelectedOU.Text;
            //    userQueryCriteriaModel.MenuID = this.MenuID;
            //    userQueryCriteriaModel.UserGroupID = this.ODMUser.UserGroupID;
            //    userQueryCriteriaModel.FlagCurrentLocation = ddlStation.SelectedValue==ODMUser.LocationID.ToString() ? 1 : 0;
            //    userQueryCriteriaModel.FlagAllLocation = (ddlStation.SelectedItem.Value == "0") ? 1 : 0;
            //    if (ddlStation.SelectedValue == ODMUser.LocationID.ToString())
            //    {
            //        userQueryCriteriaModel.LocationID = null;
            //    }
            //    else {
            //        userQueryCriteriaModel.LocationID = Convert.ToInt32(ddlStation.SelectedValue);
            //    }

            //    userQueryCriteriaModel.FlagMoreLocation = rdbmorestation.Checked ? 1 : 0;
            //    if (userQueryCriteriaModel.FlagMoreLocation == 1) {
            //        if (string.IsNullOrEmpty(this.hdnPersonHoldStationKye.Value)) {
            //            this.PAlert(this.GetMessage("SOM_PassengerFlow_AtleastOneStation"));
            //            return;
            //        }
            //        string locationIDKey = this.hdnPersonHoldStationKye.Value;
            //        if (locationIDKey.Contains(",")) { //more one location
            //            string[] locationIDArray = locationIDKey.Split(',');
            //            for (int i = 0; i < locationIDArray.Length; i++) {
            //                DataRow dr = dtLocation.NewRow();
            //                dr["UserQueryCriteriaID"] = criteriaId;
            //                dr["LocationID"] = Convert.ToUInt32(locationIDArray[i]);
            //                dtLocation.Rows.Add(dr);
            //            }
            //        }
            //        else {//one location
            //            DataRow dr = dtLocation.NewRow();
            //            dr["UserQueryCriteriaID"] = criteriaId;
            //            dr["LocationID"] = Convert.ToUInt32(locationIDKey);
            //            dtLocation.Rows.Add(dr);
            //        }

            //    }

            //    userQueryCriteriaModel.FlagDateType = rdbDateRange.Checked ? 1 : 0;
            //    userQueryCriteriaModel.DateFrom = this.dtbFrom.GetDateTime();
            //    userQueryCriteriaModel.DateTo = this.dtbTo.GetDateTime();
            //    if (userQueryCriteriaModel.FlagDateType == 1) {
            //        userQueryCriteriaModel.DateRangeType = ddlDateRange.SelectedValue;
            //    }
            //    else {
            //        userQueryCriteriaModel.DateRangeType = dtCriteria.Rows[0]["DateRangeType"].ToString();
            //    }

            //    userQueryCriteriaModel.LastUpdTime = DateTime.Now;
            //    userQueryCriteriaModel.LastUpdUser = this.ODMUser.ID;
            //    userQueryCriteriaModel.ActingasUser = this.ODMUser.ID;

            //    DAL.SOM.PassengerFlow.PassengerSelectionCriteria.UpdateCriteriaData(userQueryCriteriaModel, dtLocation);
            //    this.PAlert(this.GetMessage("SaveSuccessfully"));
            //    Session.Remove("station");
            //}
            //else {
            //    this.PAlert(this.GetMessage("SOM_PassengerFlow_CriteriaNoExists"));
            //}
            #endregion
            #region new logic
            string criteriaName = this.txtSelectedOU.Text;
            if (criteriaName.Trim() == "" || string.IsNullOrEmpty(criteriaName))
            {
                this.PAlert(this.GetMessage("SOM_PassengerFlow_CriteriaNotNullOrEmpty"));
                return;
            }
            //if (criteriaName.Trim() == "Default Criteria")
            DataTable dtDefaultCriteria = MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetDefaultCriteriaNameByMenuID(this.MenuID);
            string defaultCriteriaName = string.Empty;
            if (dtDefaultCriteria.Rows.Count > 0)
            {
                defaultCriteriaName = dtDefaultCriteria.Rows[0]["CriteriaName"].ToString();
            }
            if (criteriaName.Trim() == defaultCriteriaName)//Add by jack 20160125 ISU11 fixed for harcode 'Default Criteria'
            {
                this.PAlert(this.GetMessage("SOM_PassengerFlow_DefaultCriteriaCanNotUpdate"));
                return;
            }

            int criteriaId = 0;
            try
            {
                criteriaId = Convert.ToInt32(ddlCriteria.SelectedValue);
            }
            catch (Exception)
            {
                criteriaId = 0;
            }
            if (criteriaId > 0) //Exists 
            {
                criteriaId = ExistCriteriaName(criteriaName, this.MenuID, this.ODMUser.UserGroupID);

                if (criteriaId > 0 && Convert.ToInt32(ddlCriteria.SelectedValue) != criteriaId)
                {
                    this.PAlert(this.GetMessage("SOM_PassengerFlow_CriteriaExistCanNotUpdate"));
                    return;
                }
                else
                {
                    criteriaId = Convert.ToInt32(ddlCriteria.SelectedValue);
                }

                DataTable dtLocation = new DataTable();
                dtLocation.Columns.Add("UserQueryCriteriaID");
                dtLocation.Columns.Add("LocationID");

                DataTable dtCriteria = DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetCriteriaByCriteriaID(criteriaId);
                //if (dtCriteria.Rows.Count > 0 && dtCriteria.Rows[0]["CriteriaName"].ToString().Trim() == "Default Criteria")
                if (dtCriteria.Rows.Count > 0 && dtCriteria.Rows[0]["CriteriaName"].ToString().Trim() == defaultCriteriaName)//Add by jack 20160125 ISU11 fixed for harcode 'Default Criteria'
                {
                    this.PAlert(this.GetMessage("SOM_PassengerFlow_DefaultCriteriaCanNotUpdate"));
                    return;
                }

                Model.SOM.PassengerFlow.UserQueryCriteria userQueryCriteriaModel = new Model.SOM.PassengerFlow.UserQueryCriteria();
                userQueryCriteriaModel.UserQueryCriteriaID = criteriaId;
                userQueryCriteriaModel.CriteriaName = this.txtSelectedOU.Text;
                userQueryCriteriaModel.MenuID = this.MenuID;
                userQueryCriteriaModel.UserGroupID = this.ODMUser.UserGroupID;
                userQueryCriteriaModel.FlagCurrentLocation = ddlStation.SelectedValue == ODMUser.LocationID.ToString() ? 1 : 0;
                userQueryCriteriaModel.FlagAllLocation = (ddlStation.SelectedItem.Value == "0") ? 1 : 0;
                if (ddlStation.SelectedValue == ODMUser.LocationID.ToString())
                {
                    userQueryCriteriaModel.LocationID = null;
                }
                else
                {
                    userQueryCriteriaModel.LocationID = Convert.ToInt32(ddlStation.SelectedValue);
                }

                userQueryCriteriaModel.FlagMoreLocation = rdbmorestation.Checked ? 1 : 0;
                if (userQueryCriteriaModel.FlagMoreLocation == 1)
                {
                    if (string.IsNullOrEmpty(this.hdnPersonHoldStationKye.Value))
                    {
                        this.PAlert(this.GetMessage("SOM_PassengerFlow_AtleastOneStation"));
                        return;
                    }
                    string locationIDKey = this.hdnPersonHoldStationKye.Value;
                    if (locationIDKey.Contains(","))
                    { //more one location
                        string[] locationIDArray = locationIDKey.Split(',');
                        for (int i = 0; i < locationIDArray.Length; i++)
                        {
                            DataRow dr = dtLocation.NewRow();
                            dr["UserQueryCriteriaID"] = criteriaId;
                            dr["LocationID"] = Convert.ToUInt32(locationIDArray[i]);
                            dtLocation.Rows.Add(dr);
                        }
                    }
                    else
                    {//one location
                        DataRow dr = dtLocation.NewRow();
                        dr["UserQueryCriteriaID"] = criteriaId;
                        dr["LocationID"] = Convert.ToUInt32(locationIDKey);
                        dtLocation.Rows.Add(dr);
                    }

                }

                userQueryCriteriaModel.FlagDateType = rdbDateRange.Checked ? 1 : 0;
                //Begin  liuwenjing add 2015-09-30 issue:U224
                if (dtbFrom.Text.Trim().Length > 0 && dtbTo.Text.Trim().Length > 0)
                {
                    if (InvalidateInputDate(this.dtbFrom.GetDateTime(), this.dtbTo.GetDateTime()))
                    {
                        this.PAlert(this.GetMessage("COM_StartEndDate"));
                        return;
                    }                   
                }

                //End
                userQueryCriteriaModel.DateFrom = this.dtbFrom.GetDateTime();
                userQueryCriteriaModel.DateTo = this.dtbTo.GetDateTime();

                if (userQueryCriteriaModel.FlagDateType == 1)
                {
                    userQueryCriteriaModel.DateRangeType = ddlDateRange.SelectedValue;
                }
                else
                {
                    userQueryCriteriaModel.DateRangeType = dtCriteria.Rows[0]["DateRangeType"].ToString();
                }

                userQueryCriteriaModel.LastUpdTime = DateTime.Now;
                userQueryCriteriaModel.LastUpdUser = this.ODMUser.ID;
                userQueryCriteriaModel.ActingasUser = this.ODMUser.ID;

                DAL.SOM.PassengerFlow.PassengerSelectionCriteria.UpdateCriteriaData(userQueryCriteriaModel, dtLocation);
                this.PAlert(this.GetMessage("SaveSuccessfully"));
                Session.Remove("station");
                BindddlCriteria(this.ODMUser.UserGroupID, this.MenuID);
                this.FindByTextAndSelect(ddlCriteria, this.txtSelectedOU.Text);
            }
            else
            {
                this.PAlert(this.GetMessage("SOM_PassengerFlow_CriteriaNoExists"));
            }
            #endregion
            /*************************************/
        }

        /// <summary>
        /// Delete Criteria data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {

            string criteriaName = this.txtSelectedOU.Text.Trim();
            if (string.IsNullOrEmpty(criteriaName))
            {
                this.PAlert(this.GetMessage("SOM_PassengerFlow_CriteriaNotNullOrEmpty"));
                return;
            }
            //if (criteriaName == "Default Criteria")
            DataTable dtDefaultCriteria = MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetDefaultCriteriaNameByMenuID(this.MenuID);
            string defaultCriteriaName = string.Empty;
            if (dtDefaultCriteria.Rows.Count > 0)
            {
                defaultCriteriaName = dtDefaultCriteria.Rows[0]["CriteriaName"].ToString();
            }
            if (criteriaName == defaultCriteriaName)
            {//Default criteria
                PAlert(this.GetMessage("SOM_PassengerFlow_CommonCriteriaCanNotDelete"));
                return;
            }
            int criteriaId = ExistCriteriaName(criteriaName, this.MenuID, this.ODMUser.UserGroupID);
            if (criteriaId > 0) //Exists 
            { //delete Criteria by UserQueryCriteriaID
                try
                {
                    DataTable dtCriteria = DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetCriteriaByCriteriaID(criteriaId);

                    //if (criteriaName == "Default Criteria" || dtCriteria.Rows[0]["CriteriaName"].ToString().Trim() == "Default Criteria")
                    if (criteriaName == defaultCriteriaName || dtCriteria.Rows[0]["CriteriaName"].ToString().Trim() == defaultCriteriaName)//Add by jack 20160125 ISU11 fixed for harcode 'Default Criteria'
                    {//Default criteria
                        PAlert(this.GetMessage("SOM_PassengerFlow_CommonCriteriaCanNotDelete"));
                        return;
                    }

                    MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.DeleteCriteriaByUserQueryCriteriaID(criteriaId);
                    BindddlCriteria(this.ODMUser.UserGroupID, this.MenuID);
                    this.txtSelectedOU.Text = this.ddlCriteria.SelectedItem.Text;
                    InitCriteriaData();
                    PAlert(this.GetMessage("SOM_PassengerFlow_DeleteCriteriaSuccess"));
                }
                catch (Exception ex)
                {
                    PAlert(this.GetMessage("SOM_PassengerFlow_DeleteCriteriaFailed" + ex.Message));
                }
            }
            else
            {
                this.PAlert(this.GetMessage("SOM_PassengerFlow_CriteriaNoExistCanNotDelete"));
            }

        }

        protected void btnMorestation_Click(object sender, EventArgs e)
        {

            string criteriaID = string.Empty;
            criteriaID = ddlCriteria.SelectedValue;
            ScriptManager.RegisterStartupScript(this.updatePanel3, this.GetType(), "alert", "PersonSelectdStation('" + HttpUtility.UrlEncode(criteriaID.Trim()) + "');", true);
        }

        protected void btnChangeStation_Click(object sender, EventArgs e)
        {

            /* Comment by jack 20150827 for u181.1 */
            if (this.hdnPersonHoldStationKye.Value.Contains(','))
            {
                this.FindByValueAndSelect(ddlStation, this.hdnPersonHoldStationKye.Value.Split(',')[0]);
            }
            else
            {
                this.FindByValueAndSelect(ddlStation, this.hdnPersonHoldStationKye.Value);
            }
            /***************************************/

        }

        protected void ddlCriteria_SelectedIndexChanged(object sender, EventArgs e)
        {
            /* Comment by jack 20150827 for u181.2 */
            Session.Remove("station");
            /**************************************/
            InitCriteriaData();
            FunctionItemSetting();
        }

        protected void txtSelectedValue_OnTextChanged(object sender, EventArgs e)
        {
            /* Comment by jack 20150910 for u180.5 */
            if (txtIsJSRuning.Text.Trim() == "true")
            {
                return;
            }
            /************************************/
            //remove by gordon 20151009 U180
            //ddlCriteria_SelectedIndexChanged(null, null);

            /* Comment by jack 20150824 for u70 */
            if (ddlStation.Items.Count == 0)
            {
                ScriptManager.RegisterStartupScript(this.updatePanel3, this.GetType(), "disabled", "InitCombobox();SetCursorEnabled(true);", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.updatePanel3, this.GetType(), "disabled", "InitCombobox();SetCursorEnabled(false);", true);
            }

            //if (DAL.Security.User.CanFunctionItemAccess(this.AllAccessStationID, this.ODMUser.UserProfileID, this.MenuID))//access all station.
            //{
            //    ScriptManager.RegisterStartupScript(this.updatePanel3, this.GetType(), "disabled", "SetCursorEnabled(false);", true);
            //}
            //else if (DAL.Security.User.CanFunctionItemAccess(this.OtherAccessStationID, this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            //{
            //    ScriptManager.RegisterStartupScript(this.updatePanel3, this.GetType(), "disabled", "SetCursorEnabled(false);", true);
            //}
            //else
            //{
            //    ScriptManager.RegisterStartupScript(this.updatePanel3, this.GetType(), "disabled", "SetCursorEnabled(true);", true);
            //}
            /**********************************/
        }

        protected void ddlStation_SelectedIndexChanged(object sender, EventArgs e)
        {

            Session.Remove("station");
            this.rdbmorestation.Checked = false;
            this.rdbStation.Checked = true;
            this.hidStation.Value = ddlStation.SelectedValue;

            if (ddlStation.SelectedItem.Value == "0")
            {
                this.btnMorestation.Enabled = false;
            }

        }

        protected void ddlDateRange_SelectedIndexChanged(object sender, EventArgs e)
        {
            SettingFromToValue();
        }

        protected void rdbStation_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdbStation.Checked)
            {
                this.rdbStation.Checked = true;
                this.rdbmorestation.Checked = false;

                this.ddlStation.Enabled = true;
            }
        }

        protected void rdbmorestation_CheckedChanged(object sender, EventArgs e)
        {

            if (this.rdbmorestation.Checked)
            {
                this.rdbmorestation.Checked = true;
                this.rdbStation.Checked = false;
                this.btnMorestation.Enabled = true;
            }

        }

        protected void rdbDateRange_CheckedChanged(object sender, EventArgs e)
        {

            if (rdbDateRange.Checked)
            {
                this.ddlDateRange.Enabled = true;
                this.dtbFrom.ReadOnly = true;
                this.dtbTo.ReadOnly = true;
                SettingFromToValue();
            }

        }

        protected void rdbByDate_CheckedChanged(object sender, EventArgs e)
        {

            if (rdbByDate.Checked)
            {
                this.dtbFrom.ReadOnly = false;
                this.dtbTo.ReadOnly = false;
                this.ddlDateRange.Enabled = false;

                int CriteriaID = -10000;
                int flagUserGroup = 0;
                if (string.IsNullOrEmpty(ddlCriteria.SelectedValue))
                {
                    CriteriaID = -10000;
                    flagUserGroup = 1;
                }
                else
                {
                    CriteriaID = Convert.ToInt32(ddlCriteria.SelectedValue);
                    flagUserGroup = 0;
                }
                DataTable dtDefaultCriteria = MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetCriteriaData(CriteriaID, this.MenuID, flagUserGroup);
                if (dtDefaultCriteria.Rows.Count > 0)
                {
                    this.dtbFrom.Text = this.FormatDateTime(dtDefaultCriteria.Rows[0]["DateFrom"].ToString(), false);
                    this.dtbTo.Text = this.FormatDateTime(dtDefaultCriteria.Rows[0]["DateTo"].ToString(), false);
                }
                else
                {
                    this.dtbFrom.Text = this.FormatDateTime(this.DateFrom.ToString(), false);
                    this.dtbTo.Text = this.FormatDateTime(this.DateTo.ToString(), false);
                }
            }

        }

        #endregion

        #region Method

        public DateTime GetFromDateFromUc()
        {
            return dtbFrom.GetDateTime();
        }

        /// <summary>
        /// get from date from usercontrol psc
        /// </summary>
        /// <returns>from date</returns>
        public DateTime GetFromDateFromUcPsc()
        {
            return dtbFrom.GetDateTime();
        }

        /// <summary>
        /// get to date from usercontrol psc
        /// </summary>
        /// <returns>to date</returns>
        public DateTime GetToDateFromUcPsc()
        {
            return dtbTo.GetDateTime();
        }

        /// <summary>
        /// Get LocationID from UcPsc
        /// </summary>
        /// <returns>location id such as '1,2,3,4...'</returns>
        public string GetLocationIDFromUcPsc()
        {

            string locationIDStr = string.Empty;

            if (rdbStation.Checked)
            {
                if (ddlStation.Items.Count>0) //Add by jack 20160126 ISU17 fixed data is null, ocurs error.
                {
                    if (ddlStation.SelectedItem.Value == "0")
                    {
                        foreach (ListItem item in ddlStation.Items)
                        {
                            if (item.Value != "0" && item.Enabled == true)
                            {
                                locationIDStr += item.Value + ",";
                            }

                        }
                        locationIDStr = locationIDStr.Remove(locationIDStr.Length - 1);

                        if (locationIDStr.Length > 0)
                        {
                            return locationIDStr;
                        }
                        else
                        {
                            return "-1";
                        }
                    }
                    else
                    {
                        return this.ddlStation.SelectedValue;
                    }
                }
                else
                {
                    return "-1";
                }
                
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnPersonHoldStationKye.Value))
                {
                    return "-1";
                }
                else
                {
                    return this.hdnPersonHoldStationKye.Value;
                }
            }

        }

        /// <summary>
        /// clear ddlstation dropdownlist
        /// </summary>
        public void ClearStationDropDownlist()
        {
            this.ddlStation.Enabled = true;
            this.ddlStation.Items.Clear();
            this.ddlStation.Enabled = false;
        }

        /// <summary>
        /// set language
        /// </summary>
        public void SetLanguage()
        {
            this.lblCriteria.Text = this.GetLabel("SOM_PassengerFlow_Criteria", false);
            this.btnSave.Text = this.GetLabel("SOM_PassengerFlow_Save", false);
            this.btnSaveas.Text = this.GetLabel("SOM_PassengerFlow_Saveas", false);
            this.btnDelete.Text = this.GetLabel("SOM_PassengerFlow_Delete", false);
            this.lblStation.Text = this.GetLabel("SOM_PassengerFlow_Station", true);
            this.btnMorestation.Text = this.GetLabel("SOM_PassengerFlow_Morestation", false);
            this.lblDateRange.Text = this.GetLabel("SOM_PassengerFlow_DateRange", false);
            this.rdbByDate.Text = this.GetLabel("SOM_PassengerFlow_ByDate", false);
            this.lblFrom.Text = this.GetLabel("SOM_PassengerFlow_From", true);
            this.lblTo.Text = this.GetLabel("SOM_PassengerFlow_To", false);
        }

        public void FunctionItemSetting()
        {
            //Page Read Only
            if (DAL.Security.User.CanFunctionItemAccess(this.AllAccessStationID, this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {
                this.btnSave.Enabled = true;
                this.btnSaveas.Enabled = true;
                this.btnDelete.Enabled = true;
            }
            else if (DAL.Security.User.CanFunctionItemAccess(this.OtherAccessStationID, this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {
                this.btnSave.Enabled = true;
                this.btnSaveas.Enabled = true;
                this.btnDelete.Enabled = true;
            }
            else
            {
                /* Comment by jack 20150824 for u70 */
                BasePage_SOM bp = new BasePage_SOM();
                //update by gordon 20151024
                //if (this.MenuID == "M141001")
                //{
                //    if (!bp.IsValidStation(this.ODMUser.LocationID, true))
                //    {
                //        this.ddlStation.Items.Clear();
                //        this.btnSave.Enabled = false;
                //        this.btnSaveas.Enabled = false;
                //        this.btnDelete.Enabled = false;
                //        this.btnMorestation.Enabled = false;
                //        this.ddlStation.Enabled = false;
                //    }
                //}
                //else
                //{
                //    if (!bp.IsValidStation(this.ODMUser.LocationID))
                //    {
                //        this.ddlStation.Items.Clear();
                //        this.btnSave.Enabled = false;
                //        this.btnSaveas.Enabled = false;
                //        this.btnDelete.Enabled = false;
                //        this.btnMorestation.Enabled = false;
                //        this.ddlStation.Enabled = false;
                //    }
                //}
                if (!bp.IsValidStation(this.ODMUser.LocationID))
                {
                    this.ddlStation.Items.Clear();
                    this.btnSave.Enabled = false;
                    this.btnSaveas.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnMorestation.Enabled = false;
                    this.ddlStation.Enabled = false;
                }
                //update end 20151024
                /**********************************************/
            }
        }

        public void SettingFromToValue()
        {
            string selectValue = string.Empty;
            selectValue = ddlDateRange.SelectedValue;
            switch (selectValue)
            {
                case "D-0": //Today
                    dtbFrom.Text = this.FormatDateTime(DateTime.Now.ToString(), false);
                    dtbTo.Text = this.FormatDateTime(DateTime.Now.ToString(), false);
                    break;
                case "D-1": //Yesterday
                    dtbFrom.Text = this.FormatDateTime(DateTime.Now.AddDays(-1).ToString(), false);
                    dtbTo.Text = this.FormatDateTime(DateTime.Now.AddDays(-1).ToString(), false);
                    break;
                case "W-0": //This Week
                    if (CommonData.GetCharVal("SOM_FirstDayOfWeek") == "MON")
                    {
                        dtbFrom.Text = this.FormatDateTime(GetWeekFirstDayMon(DateTime.Now).ToString(), false);
                        dtbTo.Text = this.FormatDateTime(GetWeekFirstDayMon(DateTime.Now).AddDays(6).ToString(), false);
                    }
                    else
                    {
                        dtbFrom.Text = this.FormatDateTime(GetWeekFirstDaySun(DateTime.Now).ToString(), false);
                        dtbTo.Text = this.FormatDateTime(GetWeekFirstDaySun(DateTime.Now).AddDays(6).ToString(), false);
                    }
                    break;
                case "W-1": //Last Week
                    if (CommonData.GetCharVal("SOM_FirstDayOfWeek") == "MON")
                    {
                        dtbFrom.Text = this.FormatDateTime(GetWeekFirstDayMon(DateTime.Now).AddDays(-7).ToString(), false);
                        dtbTo.Text = this.FormatDateTime(GetWeekFirstDayMon(DateTime.Now).AddDays(-7).AddDays(6).ToString(), false);
                    }
                    else
                    {
                        dtbFrom.Text = this.FormatDateTime(GetWeekFirstDaySun(DateTime.Now).AddDays(-7).ToString(), false);
                        dtbTo.Text = this.FormatDateTime(GetWeekFirstDaySun(DateTime.Now).AddDays(-7).AddDays(6).ToString(), false);
                    }
                    break;
                case "M-0": //This Month
                    dtbFrom.Text = this.FormatDateTime(GetDateTimeMonthFirstDay(DateTime.Now).ToString(), false);
                    dtbTo.Text = this.FormatDateTime(GetDateTimeMonthLastDay(DateTime.Now).ToString(), false);
                    break;
                case "M-1": //Last Month
                    dtbFrom.Text = this.FormatDateTime(GetDateTimeMonthFirstDay(DateTime.Now).AddMonths(-1).ToString(), false);
                    dtbTo.Text = this.FormatDateTime(GetDateTimeMonthLastDay(DateTime.Now).AddMonths(-1).ToString(), false);
                    break;
                case "Y-0": //This Year
                    dtbFrom.Text = this.FormatDateTime(DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToString(), false);
                    dtbTo.Text = this.FormatDateTime(DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToString(), false);
                    break;
                case "Y-1": //Last Year
                    dtbFrom.Text = this.FormatDateTime(DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToString(), false);
                    dtbTo.Text = this.FormatDateTime(DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToString(), false);
                    break;
                default:
                    break;
            }
            this.DateFrom = dtbFrom.Text;
            this.DateTo = dtbTo.Text;
        }

        private void InitCriteriaData()
        {

            int CriteriaID = -10000; //default criteria
            int flagUserGroup = 0;
            if (string.IsNullOrEmpty(ddlCriteria.SelectedValue))
            {
                CriteriaID = -10000;
                flagUserGroup = 1;
            }
            else
            {
                CriteriaID = Convert.ToInt32(ddlCriteria.SelectedValue);
                flagUserGroup = 0;
            }

            DataTable dtDefaultCriteria = MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetCriteriaData(CriteriaID, this.MenuID, flagUserGroup);
            if (dtDefaultCriteria.Rows.Count > 0)
            {
                this.UserQueryCriteriaID = Convert.ToInt32(dtDefaultCriteria.Rows[0]["UserQueryCriteriaID"]);

                this.FlagCurrentLocation = Convert.ToInt32(dtDefaultCriteria.Rows[0]["FlagCurrentLocation"]);

                //add by gordon 20151009 U180
                this.FlagAllLocation = Convert.ToInt32(dtDefaultCriteria.Rows[0]["FlagAllLocation"]);

                BindStation();

                int flagMoreLocation = Convert.ToInt32(dtDefaultCriteria.Rows[0]["FlagMoreLocation"]);
                if (flagMoreLocation == 1)
                {
                    this.rdbmorestation.Checked = true;
                    this.btnMorestation.Enabled = true;
                    this.rdbStation.Checked = false;
                    this.ddlStation.Enabled = false;
                    this.hdnPersonHoldStationKye.Value = DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetStationStrByCriteriaID(this.UserQueryCriteriaID);
                }
                else
                {
                    this.rdbStation.Checked = true;
                    this.ddlStation.Enabled = ddlStation.Enabled && true;
                    this.rdbmorestation.Checked = false;
                    this.btnMorestation.Enabled = false;
                }

                int flagDateType = Convert.ToInt32(dtDefaultCriteria.Rows[0]["FlagDateType"]);
                if (flagDateType == 1)
                {
                    this.ddlDateRange.Enabled = true;
                    this.rdbDateRange.Checked = true;
                    this.rdbByDate.Checked = false;
                    this.DateRangeType = dtDefaultCriteria.Rows[0]["DateRangeType"].ToString().Trim();
                    BindDateRange();
                    this.dtbFrom.ReadOnly = true;
                    this.dtbTo.ReadOnly = true;
                }
                else
                {
                    dtbFrom.Text = this.FormatDateTime(dtDefaultCriteria.Rows[0]["DateFrom"].ToString(), false);
                    dtbTo.Text = this.FormatDateTime(dtDefaultCriteria.Rows[0]["DateTo"].ToString(), false);
                    this.DateRangeType = dtDefaultCriteria.Rows[0]["DateRangeType"].ToString().Trim();
                    this.FindByValueAndSelect(this.ddlDateRange, this.DateRangeType);
                    this.rdbByDate.Checked = true;
                    this.rdbDateRange.Checked = false;
                    this.dtbFrom.ReadOnly = false;
                    this.dtbTo.ReadOnly = false;
                    this.ddlDateRange.Enabled = false;
                }
            }
            else
            {
                this.PAlert(this.GetMessage("SOM_PassengerFlow_DefaultCriteriaNotFound"));
                return;
            }

        }

        private void BindddlCriteria(int UserGroupID, string MenuID)
        {

            DataTable dtCriteria = MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetCriteriaDataByGroupIDandMenuID(UserGroupID, MenuID);
            this.BindListControl(ddlCriteria, "UserQueryCriteriaID", "CriteriaName", dtCriteria, true, "");

        }

        private void BindStation()
        {

            string dblocationID = string.Empty;
            DataTable dtCriteria = DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetCriteriaByCriteriaID(this.UserQueryCriteriaID);
            if (dtCriteria.Rows.Count == 1)
            {
                if (DBNull.Value == dtCriteria.Rows[0]["UserGroupID"])
                {//Default criteria, bind current location
                    dblocationID = this.ODMUser.LocationID.ToString();
                }
                else
                {//bind db location id
                    dblocationID = dtCriteria.Rows[0]["LocationID"].ToString();
                }
            }

            //Bind Station Stop
            if (DAL.Security.User.CanFunctionItemAccess(this.AllAccessStationID, this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {
                //update by gordon 20151020 U247
                //DataTable dtStation = DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.StationID);
                DataTable dtStation = DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.LocationID);
                BindListControl(this.ddlStation, "LocationID", "station", dtStation, true);

                if (dtStation.Rows.Count == 1)
                {
                    this.rdbmorestation.Enabled = false;
                    this.rdbStation.Enabled = true;
                    this.btnMorestation.Enabled = false;
                }

                if (this.FlagCurrentLocation == 1)
                {
                    this.FindByValueAndSelect(ddlStation, this.ODMUser.LocationID.ToString());
                }
                    //add by gordon 20151009 U180
                else if (this.FlagAllLocation == 1)
                {
                    this.FindByValueAndSelect(ddlStation, "0");
                }
                else
                {
                    this.FindByValueAndSelect(ddlStation, dblocationID);
                }
                dtStation.Dispose();
            }
            else if (DAL.Security.User.CanFunctionItemAccess(this.OtherAccessStationID, this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {
                //update by gordon 20151020 U247
                //DataTable dtStation = DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID);
                DataTable dtStation = DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID);
                BindListControl(this.ddlStation, "LocationID", "station", dtStation, true);

                if (dtStation.Rows.Count == 1)
                {
                    this.rdbmorestation.Enabled = false;
                    this.rdbStation.Enabled = true;
                    this.btnMorestation.Enabled = false;
                }

                if (this.FlagCurrentLocation == 1)
                {
                    this.FindByValueAndSelect(ddlStation, this.ODMUser.LocationID.ToString());
                }
                        //add by gordon 20151009 U180
                else if (this.FlagAllLocation == 1)
                {
                    this.FindByValueAndSelect(ddlStation, "0");
                }
                else
                {
                    this.FindByValueAndSelect(ddlStation, dblocationID);
                }
            }
            else
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), false);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), false);

                this.FindByValueAndSelect(ddlStation, this.ODMUser.LocationID.ToString());
                this.ddlStation.Enabled = false;
                this.rdbmorestation.Enabled = false;
                this.rdbStation.Enabled = true;
                this.btnMorestation.Enabled = false;
            }
            this.hidStation.Value = ddlStation.SelectedValue;

            //bind more station data
            this.hdnPersonHoldStationKye.Value = DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetStationStrByCriteriaID(this.UserQueryCriteriaID);
            if (string.IsNullOrEmpty(this.hdnPersonHoldStationKye.Value))
            {
                this.hdnPersonHoldStationKye.Value = this.ODMUser.LocationID.ToString();
            }
        }

        private void BindDateRange()
        {

            DataTable dtDateRange = MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.GetDateRangeData();
            this.BindListControl(ddlDateRange, "Value", "Text", dtDateRange, false);
            this.FindByValueAndSelect(ddlDateRange, this.DateRangeType);
            SettingFromToValue();

        }

        /// <summary> 
        /// Get the first day of the week (with Sunday as the first day)
        /// </summary> 
        /// <param name="datetime"></param> 
        /// <returns></returns> 
        public DateTime GetWeekFirstDaySun(DateTime datetime)
        {
            //Sunday is the first day
            int weeknow = Convert.ToInt32(datetime.DayOfWeek);
            int daydiff = (-1) * weeknow;

            //The first day of the week
            string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");
            return Convert.ToDateTime(FirstDay);
        }

        /// <summary> 
        /// Get the first day of the week (with Monday as the first day)
        /// </summary> 
        /// <param name="datetime"></param> 
        /// <returns></returns> 
        public DateTime GetWeekFirstDayMon(DateTime datetime)
        {
            //Monday is the first day
            int weeknow = Convert.ToInt32(datetime.DayOfWeek);

            //Because Monday is the first day, so to determine weeknow equal to 0, to push forward six days.
            weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1));
            int daydiff = (-1) * weeknow;

            //The first day of the week
            string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");
            return Convert.ToDateTime(FirstDay);
        }

        /// <summary>   
        /// The first day of the month for the specified date
        /// </summary>   
        /// <param name="dateTime"></param>   
        /// <returns></returns>   
        public static DateTime GetDateTimeMonthFirstDay(DateTime dateTime)
        {

            if (dateTime == null)
            {
                dateTime = DateTime.Now;
            }

            return new DateTime(dateTime.Year, dateTime.Month, 1);
        }

        /// <summary>   
        /// Get the last day of a given month
        /// </summary>   
        /// <param name="dateTime"></param>   
        /// <returns></returns>   
        public static DateTime GetDateTimeMonthLastDay(DateTime dateTime)
        {

            int day = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);

            return new DateTime(dateTime.Year, dateTime.Month, day);
        }

        public static bool ExistCriteriaID(int CriteriaID)
        {

            int count = 0;
            MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.ExistCriteria(CriteriaID, out count);
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int ExistCriteriaName(string CriteriaName, string MenuID, int userGroupID)
        {

            int criteriaId = 0;
            MTRC.ODMS.DAL.SOM.PassengerFlow.PassengerSelectionCriteria.ExistCriterianName(CriteriaName, MenuID, userGroupID, out criteriaId);

            return criteriaId;
        }

        #endregion
        //Begin liuwenjing add 2015-09-30 issue:U224
        public bool InvalidateInputDate(DateTime DataFrom, DateTime DataTo)
        {
            if (DateTime.Compare(DataFrom, DataTo) > 0)
                return true;
            else
                return false;
        }
        //End
    }
}